//
//  ParkAnnotationView.m
//  KuWeiPark
//
//  Created by Kevin Su on 15/4/30.
//  Copyright (c) 2015年 Clief. All rights reserved.
//

#import "ParkAnnotationView.h"
#import "HomeViewController.h"

@implementation ParkAnnotationView

- (void)awakeFromNib{
    
}

- (void)initWithParkingModel:(ParkingSearchModel *)model{
    
    self.type = TYPE_ANNOTATION_PARK;
    self.parkingSearchModel = model;
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 52)];
    NSString *statusStr;
    
    if([model.isBusy intValue] == 0){
        
        imageView.image = [UIImage imageNamed:@"more_normal"];
        statusStr = @"空";
    }else{
        
        imageView.image = [UIImage imageNamed:@"less_normal"];
        statusStr = @"空";
    }
    [self addSubview:imageView];
    
    UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 12, 40, 13)];
    numberLabel.text = model.remainParkingSpace;
    numberLabel.textColor = [UIColor whiteColor];
    numberLabel.font = [UIFont systemFontOfSize:16];
    numberLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:numberLabel];
    
    UILabel *statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(11, 28, 18, 10)];
    statusLabel.text = statusStr;
    statusLabel.textColor = [UIColor whiteColor];
    statusLabel.font = [UIFont systemFontOfSize:13];
    statusLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:statusLabel];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    
    if(self.selected == selected){
        
        return;
    }
    
    if(selected){
      
        if(self.parkCallOutView == nil){
            
            self.parkCallOutView = [[[NSBundle mainBundle] loadNibNamed:@"ParkCallOutView" owner:self options:nil] lastObject];
            self.parkCallOutView.parkAnnotationView = self;
        }
        self.parkCallOutView.center = CGPointMake(self.centerOffset.x + 22, self.centerOffset.y - 45);
        [self.parkCallOutView initDataWithParkingSearchModel:self.parkingSearchModel];
        
        //设置地图的中心点
        [self.mapView setCenterCoordinate:self.annotation.coordinate animated:YES];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            self.parkCallOutView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.05, 0.05);
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.1];
            [UIView setAnimationDelegate:self];
            [UIView setAnimationDidStopSelector:@selector(bounceAnimationStopped)];
            [self addSubview:self.parkCallOutView];
            self.parkCallOutView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
            [UIView commitAnimations];
        });
        
    }else{
        
        [self.parkCallOutView removeFromSuperview];
    }
    
    [super setSelected:selected animated:animated];
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL inside = [super pointInside:point withEvent:event];
    /* Points that lie outside the receiver’s bounds are never reported as hits,
     even if they actually lie within one of the receiver’s subviews.
     This can occur if the current view’s clipsToBounds property is set to NO and the affected subview extends beyond the view’s bounds.
     */
    if (!inside && self.selected)
    {
        inside = [self.parkCallOutView pointInside:[self convertPoint:point toView:self.parkCallOutView] withEvent:event];
    }
    
    return inside;
}

- (void)bounceAnimationStopped {
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.08];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(bounce2AnimationStopped)];
    self.parkCallOutView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.95, 0.90);
    [UIView commitAnimations];
}

- (void)bounce2AnimationStopped {
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.05];
    self.parkCallOutView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
    [UIView commitAnimations];
}

@end
